package com.yzy.fly.type;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

//TODO rename relate to jdbc
public enum JavaBasicDataType {
    INT("int","int", "int"),
    INTEGER("integer","integer", "int"),
    LONG("long","long", "bigint"),
    FLOAT("float","float", "double"),
    DOUBLE("double","double", "double"),
    BIGDECIMAL("BigDecimal","BigDecimal", "double"),
    DATE("date","date", "timestamp"),
    BOOLEAN("boolean","boolean", "tinyint"),
    STRING("string","string", "varchar" ),
    JSONOBJECT("JSONObject","JSONObject", "json" ),
    JSONARRAY("JSONArray","JSONArray", "json" );

    @Setter
    @Getter
    private String type;
    @Setter
    @Getter
    private String javaType;
    @Setter
    @Getter
    private String jdbcType;

    JavaBasicDataType(String type, String javaType, String jdbcType) {
        this.type = type;
        this.javaType = javaType;
        this.jdbcType = jdbcType;
    }

    public static JavaBasicDataType type(String type) {
        if (StringUtils.isBlank(type)) {
            return null;
        }
        JavaBasicDataType result = null;
        JavaBasicDataType[] values = JavaBasicDataType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JavaBasicDataType value = values[i];
                if (value.getType().equals(type)) {
                    result = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }

    public static JavaBasicDataType javaType(String type) {
        return javaType(type, false);
    }

    public static JavaBasicDataType javaType(String type,boolean ignoreCase) {
        if (StringUtils.isBlank(type)) {
            return null;
        }
        JavaBasicDataType javaType = null;
        JavaBasicDataType[] values = JavaBasicDataType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JavaBasicDataType value = values[i];
                if(ignoreCase){
                    if (value.getJavaType().equalsIgnoreCase(type)) {
                        javaType = value;
                        break;
                    }
                } else {
                    if (value.getJavaType().equals(type)) {
                        javaType = value;
                        break;
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            javaType = null;
        }
        return javaType;
    }

    public static JavaBasicDataType jbcType(String type) {
        if (StringUtils.isBlank(type)) {
            return null;
        }
        JavaBasicDataType jdbcType = null;
        JavaBasicDataType[] values = JavaBasicDataType.values();
        try {
            for (int i = 0; i < values.length; i++) {
                JavaBasicDataType value = values[i];
                if (value.getJdbcType().equals(type)) {
                    jdbcType = value;
                    break;
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            jdbcType = null;
        }
        return jdbcType;
    }
}
